﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using Core.Authenticated;
using Core.Library;
using DataObject;
using DataObject.Category;
using DataObject.Image;
using DataObject.Product;

namespace CMS.Controls.Product
{
    public partial class CreateOrEdit : AuthenticatedControl
    {
        protected int ProductId;
        protected string ReturnUrl;
        protected string IsShowGallery = "none";

        protected void Page_Load(object sender, EventArgs e)
        {
            ProductId = Utils.GetIdFromRequestString(Utils.ProductId, 0);
            ReturnUrl = Utils.GetIdFromRequestString(Utils.ReturnUrl, string.Empty);

            //txtContent.config.toolbar = "Basic";

            if (!IsPostBack)
            {
                ltTitle.Text = "Tạo mới sản phẩm";

                InitEditor();

                ListTreeCategory();

                ListImageByProduct();

                if (ProductId > 0)
                {
                    ltTitle.Text = "Sửa thông tin sản phẩm";                    
                    BindProduct(ProductId);
                }
            }
        }

        #region Init Editor

        private void InitEditor()
        {
            //txtContent.config.language = "vi-VN";
            //txtImages.config.language  = "vi-VN";          
        }

        #endregion

        #region List Image By Product

        private void ListImageByProduct()
        {
            var lstImage = ImageController.Image_GetByProductId(ProductId);
            if (lstImage.Count > 0)
            {
                rptImage.DataSource = lstImage;
                rptImage.DataBind();

                IsShowGallery = "block";
            }
        }

        protected void rptImage_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            try
            {
                if (string.Equals(e.CommandName, "DeleteImage"))
                {
                    var id = ConvertUtility.ToInt32(e.CommandArgument);
                    if (id > 0)
                    {
                        ImageController.Image_Delete(id);
                        ltMessage.Text = MessageUtility.ShowMessage(null, "Xóa bản ghi thành công.", MessageType.Information);

                        ListImageByProduct();
                    }
                }
            }
            catch { }
        }

        #endregion

        #region Build Tree Category
        
        private void ListTreeCategory()
        {
            var categories = new List<CategoryObject>();
            CategoryController.GetListTree(categories, (int)CategoryType.Product, 0, 0);

            if (categories.Count > 0)
            {
                ddlCategory.DataSource = categories;
                ddlCategory.DataBind();
                //ddlCategory.Items.Insert(0, new ListItem("-- Danh mục --", "0"));
            }           
        }

        #endregion

        #region Product

        private void BindProduct(int id)
        {
            var o = ProductController.Product_GetById(id);
            if (o != null)
            {                
                txtCode.Text         = o.Code;
                txtName.Text         = o.Name;
                txtDescription.Text  = o.Description;
                txtContent.Text      = o.Content;
                txtUnitPrice.Text    = String.Format("{0:0.#}", o.UnitPrice);
                txtSpecialPrice.Text = String.Format("{0:0.#}", o.SpecialPrice);  
                txtImage.Text        = o.ImagePath;
                txtImages.Text       = o.Images;

                ddlCategory.SelectedValue = o.CategoryId.ToString();
                ddlStatus.SelectedValue   = o.Status.ToString();
                ddlType.SelectedValue     = o.Type.ToString();   
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            ProductObject o;
            if (ProductId > 0)
            {
                o = ProductController.Product_GetById(ProductId);
                
                o.ProductId     = ProductId;
                o.CategoryId    = o.CategoryId;
                o.Status        = ConvertUtility.ToInt32(ddlStatus.SelectedValue);
                o.Type          = ConvertUtility.ToInt32(ddlType.SelectedValue);
                o.Code          = txtCode.Text;
                o.Name          = txtName.Text;                
                o.Description   = txtDescription.Text;
                o.ImagePath     = txtImage.Text;
                o.Images        = txtImages.Text;
                o.Content       = txtContent.Text;
                o.CategoryId    = ConvertUtility.ToInt32(ddlCategory.SelectedValue);
                o.UnitPrice     = ConvertUtility.ToDecimal(txtUnitPrice.Text);
                o.SpecialPrice  = ConvertUtility.ToDecimal(txtSpecialPrice.Text);       
                o.EditedBy      = CurrentUser.AdminUserId.ToString();

                ProductController.Product_Update(o);                
            }
            else
            {
                o = new ProductObject();
                o.CategoryId    = o.CategoryId;
                o.Status        = ConvertUtility.ToInt32(ddlStatus.SelectedValue);
                o.Type          = ConvertUtility.ToInt32(ddlType.SelectedValue);
                o.Code          = txtCode.Text;
                o.Name          = txtName.Text;                
                o.Description   = txtDescription.Text;
                o.ImagePath     = txtImage.Text;
                o.Images        = txtImages.Text;
                o.Content       = txtContent.Text;
                o.CategoryId    = ConvertUtility.ToInt32(ddlCategory.SelectedValue);
                o.UnitPrice     = ConvertUtility.ToDecimal(txtUnitPrice.Text);
                o.SpecialPrice  = ConvertUtility.ToDecimal(txtSpecialPrice.Text);       
                o.CreatedBy     = CurrentUser.AdminUserId.ToString();

                ProductController.Product_Create(o);
            }

            Response.Redirect(!string.IsNullOrEmpty(ReturnUrl) ? HttpUtility.HtmlDecode(ReturnUrl) : UrlHelpers.ListProduct());
        }

        #endregion
    }
}